package com.thinvent.remotepassword.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thinvent.remotepassword.mapper.IppasswordMapper;
import com.thinvent.remotepassword.entity.Ippassword;
import com.thinvent.remotepassword.service.IppasswordService;
import com.thinvent.remotepassword.util.ThreadPoolUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * (Ippassword)表服务实现类
 *
 * @author makejava
 * @since 2024-06-06 16:46:19
 */
@Service("ippasswordService")
public class IppasswordServiceImpl extends ServiceImpl<IppasswordMapper, Ippassword> implements IppasswordService {

    @Override
    public void task(Ippassword ippassword) {
        ScheduledExecutorService scheduledExecutorService = ThreadPoolUtils.newScheduledThreadPool(3);
        Runnable task = ()->{
            Ippassword one = this.getOne(new QueryWrapper<>(new Ippassword(){{
                setHostaddress(ippassword.getHostaddress());
            }}));
            if(StringUtils.isBlank(one.getRemoteaddress())){
                one.setBindaddress("");
                this.updateById(one);
            }else {
                Runnable task1 = ()->{
                    Ippassword one1 = this.getOne(new QueryWrapper<>(new Ippassword(){{
                        setHostaddress(ippassword.getHostaddress());
                    }}));
                    one1.setBindaddress("");
                    this.updateById(one1);
                };
                scheduledExecutorService.schedule(task1, 19*60, TimeUnit.SECONDS);

            }
        };
        scheduledExecutorService.schedule(task, 60, TimeUnit.SECONDS);

    }
}

